home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sources / xbmbrows / xbmbro31.z / xbmbro31 / xbmbrowser3.1 / xbmbrowser.c < prev    next >
C/C++ Source or Header  |  1993-08-05  |  9KB  |  263 lines

  1. char copyright[]="xbmbrowser Version 3.1  (c) Copyright Ashley Roll, 1992.";
  2. /*
  3. *****************************************************************************
  4. ** xbmbrowser version 3.1  (c) Copyright Ashley Roll, 1992.
  5. ** FILE: xbmbrowser.c
  6. **
  7. ** xbmbrowser is Public Domain. However it, and all the code still belong to me.
  8. ** I do, however grant permission for you to freely copy and distribute it on 
  9. ** the condition that this and all other copyright notices remain unchanged in 
  10. ** all distributions.
  11. **
  12. ** This software comes with NO warranty whatsoever. I therefore take no
  13. ** responsibility for any damages, losses or problems that the program may 
  14. ** cause.
  15. *****************************************************************************
  16. */
  17.  
  18. #define MAIN
  19. #include "xbmbrowser.h"
  20. #include "patchlevel.h"
  21. #include "user-menu.h"
  22. #include "icon.xbm"
  23.  
  24. /* -------------------------- */
  25. static Atom  wm_delete_window;      /* insure that delete window works */
  26.  
  27. static XtActionsRec  actions[] = {  /* declare possible actions */
  28. /* action_name, routine */
  29.   { "quit",      DoQuit  },
  30.   { "CD",        change_dir },
  31.   { "Set_Name",  set_name },
  32.   { "Set_Label", set_label },
  33.   { "Ok",        Ok },
  34.   { "Pos_Dir",   pos_dir },
  35. };
  36.  
  37. /* translation table for label widgets for the bitmaps */
  38. char Translations[] = 
  39.   "<EnterWindow>:  Set_Name() \n\
  40.    <LeaveWindow>:  Set_Label() \n\
  41.    <BtnDown>:    XawPositionSimpleMenu(bitmapMenu) MenuPopup(bitmapMenu) \n\
  42.    <BtnUp>:      MenuPopdown(bitmapMenu)";
  43.  
  44. /* translation table for the dialogWidget (directory name) */
  45. static char text_trans[] = 
  46.   "<Key>Return:  CD() \n\
  47.    Ctrl<Key>M:   CD() \n\
  48.    <Btn3Down>:  Pos_Dir() MenuPopup(DirPopup) ";
  49.  
  50. /*  translation table for the list widget */
  51. static char list_trans[] =
  52.    "<Enter>:      Set() \n\
  53.     <Leave>:      Unset() \n\
  54.     <BtnMotion>:  Set() \n\
  55.     <BtnUp>:      MenuPopdown(DirPopup) Notify() Unset()"; 
  56.  
  57. /*  translation table for the transient shell containing the list */
  58. /* this translation makes the list popdown if the button is released 
  59.    outside the window */
  60. static char tshell_trans[] = 
  61.        "<BtnUp>: MenuPopdown(DirPopup)";
  62.  
  63. /* fallback resources  */
  64. static char *fall_back[] = {
  65.    ".XbmBrowser.width: 450", 
  66.    ".XbmBrowser.height: 500", 
  67.    ".XbmBrowser.iconName: browser",
  68.    ".XbmBrowser.?.TransientShell.width: 350",
  69.    ".XbmBrowser.main.buttons.Command.width: 70",
  70.    NULL
  71. }; 
  72.  
  73. /* -------------------------- */
  74.  
  75. main(argc, argv)
  76.   int argc;
  77.   char **argv;
  78. {
  79. XtAppContext appcon;
  80. Widget button;
  81.  
  82.   ac = argc;
  83.   av = argv;
  84.   dialogs_made = FALSE;
  85.   directory = dname;
  86.   bitmap_info = NULL;
  87.  
  88.   /* Setup the toplevel window */
  89.   {
  90.    Pixmap icon;
  91.    char title[80];
  92.         
  93.       sprintf(title,"XbmBrowser Version %s",PATCHLEVEL);
  94.  
  95.       toplevel = XtVaAppInitialize(
  96.                  &appcon, "XbmBrowser",    /* app context, ClassName */
  97.                  NULL, 0,                  /* app command line options */
  98.                  &argc, argv,              /* command line */
  99.                  fall_back,                /* Fall back resources */
  100.                  XtNtitle,(XtArgVal)title,
  101.                  NULL);                    /* End Va resource list */
  102.  
  103.       icon = XCreateBitmapFromData(
  104.                  XtDisplay(toplevel), RootWindowOfScreen(XtScreen(toplevel)),
  105.                  (char *)icon_bits, icon_width, icon_height);
  106.  
  107.       XtVaSetValues(toplevel,XtNiconPixmap, icon,NULL);
  108.  
  109.   }
  110.   XtAppAddActions(appcon, actions, XtNumber(actions));
  111.  
  112.   if( argc > 2 )  useage();
  113.   if(argc == 2) {
  114.     if(chdir(argv[1]) != 0) {
  115.       fprintf(stderr,"xbmbrowser: couldn't chdir to '%s'\n",argv[1]);
  116.       exit(0);
  117.     }
  118.   }
  119.  
  120.   (void) getcwd(dname,253);
  121.  
  122.   /* create the cursors */
  123.   normalCursor = XCreateFontCursor(XtDisplay(toplevel),XC_left_ptr);
  124.   waitCursor = XCreateFontCursor(XtDisplay(toplevel),XC_watch);
  125.  
  126.   /* create a paned widget to put everything into */
  127.   mainpw  = XtVaCreateManagedWidget("main",panedWidgetClass,toplevel,
  128.             XtNcursor,(XtArgVal)normalCursor,
  129.             NULL);
  130.  
  131.   /* create the form widget to put the buttons in */
  132.   bfw = XtVaCreateManagedWidget("buttons",formWidgetClass,mainpw,
  133.               XtNshowGrip,(XtArgVal)False,
  134.               XtNskipAdjust,(XtArgVal)True,NULL);
  135.  
  136.   /* create the Buttons */
  137.   button = XtVaCreateManagedWidget("quit",commandWidgetClass,bfw,
  138.                   XtNlabel,(XtArgVal)"Quit",NULL);
  139.   XtAddCallback(button,"callback",DoQuit,NULL);
  140.   button = XtVaCreateManagedWidget("help",commandWidgetClass,bfw,
  141.                   XtNfromHoriz,(XtArgVal)button,
  142.                   XtNlabel,(XtArgVal)"Help",NULL);
  143.   XtAddCallback(button,"callback",Show_Help,NULL);
  144.   button = XtVaCreateManagedWidget("rescan",commandWidgetClass,bfw,
  145.                   XtNfromHoriz,(XtArgVal)button,
  146.                   XtNlabel,(XtArgVal)"Rescan",NULL);
  147.   XtAddCallback(button,"callback",rescan,NULL);
  148.  
  149.   /* dialogWidget to hold and get the directory to read from */
  150.   atw = XtVaCreateManagedWidget("directory", dialogWidgetClass, mainpw,
  151.            XtNlabel,(XtArgVal)"Current Directory",
  152.            XtNvalue,(XtArgVal)dname,
  153.            XtNshowGrip,(XtArgVal)False,
  154.            XtNskipAdjust,(XtArgVal)True,
  155.            NULL);
  156.  
  157.   XtOverrideTranslations(XtNameToWidget(atw,"value"),
  158.                          XtParseTranslationTable(text_trans));
  159.  
  160.   /* create the dirMenu list widget in a popup shell. */
  161.   dirPopup =  XtVaCreatePopupShell("DirPopup",transientShellWidgetClass,atw,
  162.               XtNoverrideRedirect,(XtArgVal)True,
  163.               XtNallowShellResize,(XtArgVal)True,
  164.               NULL);
  165.  
  166.   XtOverrideTranslations(dirPopup,XtParseTranslationTable(tshell_trans));
  167.  
  168.   dirList = XtVaCreateManagedWidget("DirList",listWidgetClass,dirPopup,
  169.                   NULL);
  170.  
  171.   XtAddCallback(dirList,"callback",dir_menu,NULL);
  172.  
  173.   XtOverrideTranslations(dirList,XtParseTranslationTable(list_trans));
  174.  
  175.  
  176.   /* label widget to hold the name of the current bitmap */
  177.   strcpy( label_info, "Loading Bitmaps" );
  178.   lw = XtVaCreateManagedWidget("label",labelWidgetClass,mainpw,
  179.                   XtNlabel,      (XtArgVal)label_info,
  180.                   XtNshowGrip,   (XtArgVal)False,
  181.                   XtNskipAdjust, (XtArgVal)True,NULL);
  182.  
  183.   /* create a viewport widget to stick the bitmaps in */
  184.   { int width;
  185.  
  186.     XtVaGetValues(toplevel,XtNwidth,&width,NULL);
  187.  
  188.     phw = XtVaCreateManagedWidget("viewport",viewportWidgetClass,mainpw,
  189.                   XtNwidth,(XtArgVal)width,
  190.                   XtNallowHoriz,(XtArgVal)False,
  191.                   XtNallowVert,(XtArgVal)True,
  192.                   XtNshowGrip,(XtArgVal)False,
  193.                   XtNskipAdjust,(XtArgVal)True,NULL);
  194.  
  195.   }
  196.  
  197.   /* setup the user menu */
  198.   { char rcfile[256];
  199.     FILE *f;
  200.  
  201.     /* try to find the HOME_DIR_FILE */
  202.     sprintf(rcfile,"~/%s",HOME_DIR_FILE);
  203.     expand_tilder(rcfile);
  204.  
  205.     if((f = fopen(rcfile,"r")) == (FILE *) NULL) {
  206.       /* try the DEFAULT_RC_FILE */
  207.       strcpy(rcfile,DEFAULT_RC_FILE);
  208.   
  209.       if((f = fopen(rcfile,"r")) == (FILE *) NULL) {
  210.         fprintf(stderr,"xbmbrowser: Can not find a menu definition file\n"); 
  211.         fprintf(stderr,"  looking for: ~/%s \n  or %s\n",
  212.                  HOME_DIR_FILE, DEFAULT_RC_FILE);
  213.         DoQuit();
  214.       } 
  215.     }
  216.     fclose(f); 
  217.     Create_user_menu(phw ,"bitmapMenu",rcfile);
  218.   }
  219.  
  220.   /* setup the bow to hold all the bitmaps */
  221.   { int width,fore,back,depth;
  222.  
  223.     XtVaGetValues(toplevel,XtNwidth,&width,NULL);
  224.  
  225.     /* create the box widget to put all the bitmaps in */
  226.     bw = XtVaCreateManagedWidget("bitmaps",boxWidgetClass,phw,
  227.                     XtNwidth,width,
  228.                     XtNorientation,(XtArgVal)XtorientVertical,
  229.                     XtNfromVert,(XtArgVal)lw,
  230.                     NULL);
  231.  
  232.     /* find and set the stipple background pattern */
  233.     XtVaGetValues(bw, XtNborderColor, &fore,
  234.                       XtNbackground,  &back,
  235.                       XtNdepth,       &depth, NULL);
  236.     stipple = XmuCreateStippledPixmap(XtScreen(phw), fore, back, depth);
  237.     XtVaSetValues(bw, XtNbackgroundPixmap, (XtArgVal)stipple, NULL);
  238.   }
  239.  
  240.   scan_bitmaps();
  241.  
  242.   XtRealizeWidget(toplevel);
  243.  
  244.   /* Set the window to call quit() action if `deleted' */
  245.   XtOverrideTranslations(toplevel,     
  246.           XtParseTranslationTable("<Message>WM_PROTOCOLS:quit()") );
  247.   wm_delete_window = XInternAtom(XtDisplay(toplevel),"WM_DELETE_WINDOW",False);
  248.   (void) XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel),
  249.                                                  &wm_delete_window, 1);
  250.   XtAppMainLoop(appcon);
  251.  
  252. exit(0);
  253. }
  254.  
  255.  
  256. useage()
  257. {
  258.   fprintf(stderr,"\nxbmbrowser: Usage: \nxbmbrowser [toolkit options] [<Dir>]\n");
  259.   fprintf(stderr,"\nWhere <Dir> is a directory name. \n");
  260.   exit(1);
  261. }
  262.  
  263.